#include "keyboard.h"
CKeyboard k;
CKeyboard::CKeyboard()
{
  char name[256] = "Unknown";
  char device[256] ="/dev/input/event1";
#ifdef ARM32
  //Open Device
  datasize = sizeof (struct input_event);
  if ((fd = open (device, O_RDONLY)) == -1)
  {
    qDebug() << "is not a valid device." <<  device;
    return;
  }
  int rep[2];
  rep[0] = 0; // delay
  rep[1] = 0;
  int re[2];
  //Print Device Name
  ioctl(fd, EVIOCGNAME(sizeof (name)), name);
  qDebug() << "Reading From:" << device << name;
  ioctl(fd, EVIOCGPHYS(sizeof (name)), name);
  qDebug() << "Reading From:" << name;
  ioctl(fd, EVIOCGID, (sizeof (name)), name);
  qDebug() << "Reading From:" << name;
  ioctl(fd, EVIOCGREP, re);
  qDebug() << "Reprate:" << re[0] << re[1];
  ioctl(fd, EVIOCSREP, rep);
  ioctl(fd, EVIOCGREP,re);
  qDebug() << "Reprate:" << re[0] << re[1];
  ioctl(fd,  (sizeof (name)), name);
  ioctl(fd,  (sizeof (name)), name);
#endif
  qDebug() << "Reading From:" << device << name;
}

void CKeyboard::run()
{
  exec();
}

void CKeyboard::exec()
{
  //qDebug() << "KEY START";
  if(!fd)
    return;
#ifdef ARM32
  while(1)
  {
    if ((rd = read (fd, ev, datasize * 64)) >= datasize)
    {
      value = ev[0].value;
  //    qDebug() << "Wert:" << value << ev[0].type << ev[0].code << ev[1].value << ev[1].type << ev[1].code;
      
      if (value != ' ' && ev[1].value == 0 && ev[1].type == 1)
      {
         //qDebug() << "KEY UP" <<  ev[1].code;
         int c = ev[1].code;
         s.ckeys.removeOne(c);
         if(s.keys.contains(c))
           s.keys.removeOne(c);
         else
           s.keys.append(c);

         switch(ev[1].code)
         {
           case 1:
             s.state = CANCELED;
             //qDebug() << "CANC";
             break;
           case 2:
           case 3:
           case 4:
           case 5:
           case 6:
           case 7:
           case 8:
           case 9:
           case 10:
           case 11:
             s.speed = 11-ev[1].code;
             break;
 /*        case 25:  // PAUSE
             if(s.state != PAUSE)
             {
               vorpause = s.state;
               s.state = PAUSE;
             }
             else
               s.state = vorpause;
             break;
           case 31: 
             if(s.state == PLOTTING)
               s.state = RPLOTTING;
             else if(s.state==RPLOTTING)
               s.state = PLOTTING;
             break;
           case 103:
           case 104:
           case 105:
           case 106:
           case 108:
           case 109:
             s.state = altstate;
             break;
           default:
            qDebug() << "UN KEY" <<  ev[1].code;*/
         }
      }
      if (value != ' ' && ev[1].value == 1 && ev[1].type == 1)
      {
         //qDebug() << "KEY DOWN" <<  ev[1].code;
        // altstate = s.state;
         s.ckeys.append(ev[1].code);
/*         switch(ev[1].code)
         {
         case 2:
         case 3:
         case 4:
         case 5:
         case 6:
         case 7:
         case 8:
         case 9:
         case 10:
             s.speed = (11-ev[1].code)*1000;
             break;
         case 105:
            s.state = MANU;
            //s.manu(TX,true);
           // s.start();
           break;
         case 106:
            s.state = MANU;
            //s.manu(TX,false);
           // s.start();
           break;
         case 108:
            s.state = MANU;
            //s.line(0,9999999,0);
            //s.start();
           break;
         case 103:
            s.state = MANU;
            //s.line(0,-9999999,0);
            //s.start();
           break;
         case 104:
            s.state = MANU;
            //s.line(0,0,-9999999);
            //s.start();
           break;
         case 109:
            s.state = MANU;
            //s.line(0,0,9999999);
            //s.start();
           break;
        }*/
      }
    }
  }
  //qDebug() <<"KEY ENDS";
#endif
}
